package com.bocweb.fly.jiaodao.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bocweb.fly.jiaodao.MainAct;
import com.bocweb.fly.jiaodao.R;
import com.bocweb.fly.jiaodao.utils.SegmentControl;
import com.milk.base.BaseFragment;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by fly on 16/10/10.
 */
public class FmMyBusiness extends BaseFragment {

    private Fragment mTab01;
    private Fragment mTab02;
    @Bind(R.id.rel_my_create)
    RelativeLayout relMyCreate;
    @Bind(R.id.rel_my_jr)
    RelativeLayout relMyJr;
    @Bind(R.id.txt_my_create)
    TextView txtMyCreate;
    @Bind(R.id.txt_my_jr)
    TextView txtMyJr;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_business;
    }

    @Override
    protected boolean flux() {
        return false;
    }

    @Override
    public void initData() {
        setSelect(0);
    }

    private void setSelect(int i) {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                if (mTab01 == null) {
                    mTab01 = new FmMyCreate();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    // Fragment already added:
                    if (mTab01.isAdded()) {
                        transaction.show(mTab01);
                    }else {
                        transaction.remove(mTab01).commitAllowingStateLoss();
                        mTab01 = new FmMyCreate();
                        transaction.add(R.id.id_content, mTab01);
                    }
                }
                txtMyCreate.setTextColor(getResources().getColor(R.color.btn_oragne_press));
                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new FmMyCreate();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    // Fragment already added:
                    if (mTab02.isAdded()) {
                        transaction.show(mTab02);
                    }else {
                        transaction.remove(mTab02).commitAllowingStateLoss();
                        mTab02 = new FmMyCreate();
                        transaction.add(R.id.id_content, mTab02);
                    }
                }
                txtMyJr.setTextColor(getResources().getColor(R.color.btn_oragne_press));
                break;
        }
        transaction.commitAllowingStateLoss();
    }
    private void resetImg() {
        txtMyJr.setTextColor(getResources().getColor(R.color.txt_sy));
        txtMyCreate.setTextColor(getResources().getColor(R.color.txt_sy));
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
            transaction.hide(mTab01);
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
        }
    }
    @OnClick({R.id.rel_my_jr,R.id.rel_my_create})
    public void onClick(View view){
        resetImg();
        switch (view.getId()){
            case R.id.rel_my_jr:
                setSelect(1);
                break;
            case R.id.rel_my_create:
                setSelect(0);
                break;

        }
    }
}

